const MOB_TYPES = {
  PLAYER: 1,
  MONSTER: 2,
  BULLET: 3,
  FIRE_BALL: 4
}
const HP_INCREASED_BY = {
  HART: 50, // 吃加血道具恢复50HP
  MONSTER: -30, // 直接碰到怪物减少30HP
  FIRE_BALL: -40, // 被火球击中减少40HP
  BULLET: -20 // 被子弹击中减少20HP
}
const HP_INCREASED_REASON = Object.fromEntries(Object.keys(HP_INCREASED_BY).map(o=>[o,o]))
const PLAYER_INIT_HP = 100; // 玩家初始1000HP
const PLAYER_MOVING_STEP = 3 // 玩家每tick移动多少距离，单位px
const MONSTER_MOVING_STEP = PLAYER_MOVING_STEP / 2 // 怪兽每tick移动多少距离，单位px
const MONSTER_SHOT_DELAY = 5000 // 怪兽发射投射物的时间间隔,单位毫秒
const MONSTER_PROJECTILE_PER_SHOT = 8 // 怪兽会一次发射多少个投射物
const MONSTER_PROJECTILE_DELAY_PER_SHOT = 500 // 多个投射物之间的发射时间间隔,单位毫秒
const KEY_DOWN_HANDLER = Symbol('keydownHandler')
const KEY_UP_HANDLER = Symbol('keyuphandler')
// 用户控制方向时移动角度对应关系：1、2、3、4位代表上、下、左、右
const PLAYER_DIRECTION = {
  '0000': undefined,
  '0001': 360 - 0,
  '0010': 360 - 180,
  '0011': undefined,
  '0100': 360 - 270,
  '0101': 360 - 315,
  '0110': 360 - 225,
  '0111': 360 - 270,
  '1000': 360 - 90,
  '1001': 360 - 45,
  '1010': 360 - 135,
  '1011': 360 - 90,
  '1100': undefined,
  '1101': 360 - 0,
  '1110': 360 - 180,
  '1111': undefined
}

function getRectByPosition(x,y,width,height){
  return {
    leftTop: {x: x, y: y},
    rightTop: {x: x + width, y: y},
    leftBottom: {x: x, y: y + height},
    rightBottom: {x: x + width, y: y + height}
  }
}
function getAngle(player, monster) {
  let angle = 0;
  const {x: pX, y: pY, height: pH, width: pW} = player
  const {x: mX, y: mY, height: mH, width: mW} = monster
  // 中心点坐标 c - center, m - monster, p - player
  let cmY = mY + mH / 2, 
      cmX = mX + mW / 2, 
      cpY = pY + pH / 2, 
      cpX = pX + pW / 2
  let x = cpX - cmX, y = cpY - cmY
  let targetX,targetY
  if(Math.abs(x) <= monster.speed){
    targetX = pX - (mW - pW) / 2
  }
  if(Math.abs(y) <= monster.speed){
    targetY = pY - (mH - pH) / 2
  }

  // 计算弧度
  if(x !== 0) {
    angle = Math.atan(y / x) * 180 / Math.PI
    if(y === 0){
      angle = x > 0 ? 0 : 180
    }else if(x < 0) {
      angle = 180 + angle
    }
  }else {
    angle = y > 0 ? 90 : 270    
  }
  return {
    angle,
    targetX,
    targetY
  };
}